package com.lcq.gulimall.product.app;

import com.lcq.common.vo.UserShowInfoVo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping("read")
    public String read(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("lock");
        RLock rLock = lock.readLock();
        String res = null;
        System.out.println("reading!! "+Thread.currentThread().getName()+" id:"+Thread.currentThread().getId());
        try {
            rLock.lock();
            System.out.println("read lock!!");
            Thread.sleep(6000);
            //TimeUnit.SECONDS.sleep(6);
            res = redisTemplate.opsForValue().get("test");
        }catch (Exception e){

        }finally {
            rLock.unlock();
            System.out.println("read unlock!!");
        }
        return res;
    }

    @RequestMapping("write")
    public String write(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("lock");
        RLock rLock = lock.writeLock();
        try {
            rLock.lock();
            System.out.println("write lock!!");
            Thread.sleep(6000);
            //TimeUnit.SECONDS.sleep(3);
            redisTemplate.opsForValue().set("test",new Date().toString());
        }catch (Exception e){

        }finally {
            rLock.unlock();
            System.out.println("write unlock!!");
        }
        return "OK";
    }

    @RequestMapping("getNull/{id}")
    @ResponseBody
    @Cacheable(value = "test",key = "'getNull'")
    public String getNull(@PathVariable("id")Long id){
        if (id==1){
            return null;
        }
        return "hello:"+id;
    }

    @RequestMapping("getTime")
    @ResponseBody
    @Cacheable(value = "test",key = "'getTime'")
    public String getTime(){
        return new Date().toString();
    }

    @RequestMapping("delTime")
    @ResponseBody
    @CacheEvict(value = "test",key = "'getTime'")
    public String delTime(){
        return new Date().toString();
    }

    @RequestMapping("session/test")
    public String sessionTest(HttpSession session){
        UserShowInfoVo vo = (UserShowInfoVo) session.getAttribute("user");
        return vo.getUsername();
    }
}
